<template>
    <div class="box">
        <div class="box-card">
            <el-card class="login-left" shadow="never">
                <div class="img-box">
                    <img class="img" src="https://www.ptu.edu.cn/images/logo.png" alt="莆田学院标志">
                </div>
                <div class="login-text">
                    <el-form label-position="right" label-width="auto" style="max-width: 600px" ref="ruleFormRef"
                        :model="loginData" :rules="rules" @keyup.native.enter="login(loginData)">
                        <div class="account">
                            <el-form-item label="账号:" class="form-label" prop="userName">
                                <input v-model="loginData.userName" placeholder="请输入账号" class="input-account" />
                            </el-form-item>
                        </div>
                        <div class="passw">
                            <el-form-item label="密码:" class="form-label" prop="password">
                                <input v-model="loginData.password" :type="passwordType" placeholder="请输入密码"
                                    data="passworedtype" class="input-passw" />
                                <span @click="changeEyes">
                                    <img src="@/assets/images/闭眼.png" class="conceal" v-if="!eyes" />
                                    <img src="@/assets/images/睁眼.png" class="conceal" v-if="eyes" />
                                </span>
                            </el-form-item>
                        </div>
                        <div class="captcha">
                            <el-form-item label="验证码:" class="form-label" prop="captcha">
                                <input v-model="loginData.captcha" placeholder="输入验证码" class="input-captcha" />
                                <img :src="codeValue" alt="" class="captcha-img" @click="changeCaptcha" />
                            </el-form-item>
                        </div>
                        <div class="btn">
                            <el-button type="primary" round class="login-btn" @keyup.enter="login(loginData)"
                                @click="login(loginData)">登录</el-button>
                        </div>
                    </el-form>
                </div>
            </el-card>
            <div class="login-right">
                <div class="switch_circle"></div>
                <div class="switch_circle switch_circle-t"></div>
                <div class="text">
                    <!-- <div>欢迎回来！</div>
                    <div>请登录</div> -->
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router'
import axios from "axios";


const router = useRouter()
const route = useRoute()

//验证码图片
const codeValue = ref('')
//密码小眼睛
const eyes = ref(false)
const passwordType = ref('password')

const loginData = reactive({
    //账号
    userName: '',
    //密码
    password: '',
    //验证码(输入的)
    captcha: '',
    //验证码值
    codeKey: '',
})

const ruleFormRef = ref(null)
const rules = reactive({
    userName: [
        { required: true, message: '请填写用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请填写密码', trigger: 'blur' },
    ],
    captcha: [
        { required: true, message: '请填写验证码', trigger: 'blur' },
    ],
})

onMounted(() => {
    //获取验证码图片
    generateValidateCode()
})

//改变密码小眼睛
const changeEyes = () => {
    if (eyes.value == false) {
        passwordType.value = 'text'
    }
    if (eyes.value == true) {
        passwordType.value = 'password'
    }
    eyes.value = !eyes.value
}

//获取验证码图片
const generateValidateCode = () => {
    axios.get('/api/ac/system/index/generateValidateCode').then(res => {
        console.log(res.data.data);
        codeValue.value = res.data.data.codeValue
        loginData.codeKey = res.data.data.codeKey
    })
}
//登录
const login = (data) => {
    ruleFormRef.value.validate((valid) => {
        if (valid) {
            axios.post('/api/ac/system/index/login',data).then(res => {
                console.log(res.data.code);
                if (res.data.code == 200) {
                    ElMessage.success('登录成功')
                    localStorage.setItem('token', res.data.data.token)
                    router.push({
                        name: 'frontHome'
                    })
                } else {
                    ElMessage.error('登录失败')
                    generateValidateCode()
                }
            })
        } else {
            ElMessage.warning('请填写完整')
        }
    });

}
//点击图片重新获取验证码
const changeCaptcha = () => {
    generateValidateCode()
}

</script>


<style lang="less">
.form-label .el-form-item__label {
    font-size: 15px;
    font-weight: 550;
    color: #697076;
}
</style>


<style lang="less" scoped>
.box {
    min-width: 100vw;
    min-height: 100vh;
    margin: 0;
    padding: 0;
    background-color: #ecf0f3;
    display: flex;
    justify-content: center;
    position: absolute;

    .box-card {
        position: relative;
        display: flex;
        justify-content: space-between;
        margin: auto;
        min-width: 1100px;
        height: 570px;
        border-radius: 20px;
        background-color: #ecf0f3;
        box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
        border-radius: 12px;
        overflow: hidden;


        .login-left {
            width: 700px;
            height: 100%;
            margin-top: 40px;
            padding: 0;
            border-radius: 20px 0 0 20px;
            border: none;
            display: flex;
            justify-content: center;
            background-color: #ecf0f3;
            z-index: 100;

            .img-box {
                overflow: hidden;
            }

            .img {
                overflow: hidden;
                width: 300px;
                margin-left: 30px;
                color: #4a8de0;
                transform: translateY(-200px);
                filter: drop-shadow(0 220px);
            }


            .login-text {
                width: 100%;
                margin-top: 40px;

                .account {
                    margin-top: 15px;

                    .input-account {}
                }

                .passw {
                    margin-top: 16px;
                    display: flex;

                    .input-passw {
                        // position: absolute;
                    }

                    .conceal {
                        position: absolute;
                        top: 6px;
                        left: 285px;
                        width: 24px;
                        height: 24px;
                        // background-image: url(@/assets/images/闭眼.png);
                        background-size: 100% 100%;
                        cursor: pointer;
                        color: #b8bec7;
                    }

                }

                .captcha {
                    margin-top: 5px;
                    display: flex;

                    .input-captcha {
                        width: 180px;
                    }

                    .captcha-img {
                        width: 100px;
                        margin-left: 20px;
                        border-radius: 5px;
                    }
                }

                input {
                    height: 35px;
                    width: 300px;
                    padding: 0 10px;
                    border: none;
                    outline: none;
                    background-color: #ecf0f3;
                    transition: 0.25s ease;
                    border-radius: 8px;
                    box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
                }

                input:focus {
                    box-shadow: inset 4px 4px 4px #d1d9e6, inset -4px -4px 4px #f9f9f9;
                }

                .el-form-item__label {
                    color: red;
                }

            }

            .btn {
                display: flex;
                justify-content: center;
            }

            .login-btn {
                margin-top: 30px;
                width: 240px;
                background-color: #4a8de0;

            }
        }



        .login-right {
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            top: 0;
            right: 0;
            height: 100%;
            width: 380px;
            transition: 1.25s;
            background-color: #ecf0f3;
            overflow: hidden;
            box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #d1d9e6;

            .switch_circle {
                position: absolute;
                width: 500px;
                height: 500px;
                border-radius: 50%;
                background-color: #ecf0f3;
                box-shadow: inset 8px 8px 12px #b8bec7, inset -8px -8px 12px #fff;
                top: 60%;
                left: -90%;
                transition: 1.25s;
            }

            .switch_circle-t {
                top: -30%;
                left: 60%;
                width: 300px;
                height: 300px;
            }

            .text {
                text-align: center;
                font-size: 20px;
                font-weight: 900;
            }


        }
    }
}
</style>